package com.ai.demo.tools;

import com.ai.demo.entity.Course;
import com.ai.demo.entity.CourseReservation;
import com.ai.demo.entity.School;
import com.ai.demo.entity.query.CourseQuery;
import com.ai.demo.service.ICourseReservationService;
import com.ai.demo.service.ICourseService;
import com.ai.demo.service.ISchoolService;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@RequiredArgsConstructor
@Component
public class CourseTools {

    private final ICourseService courseService;

    private final ISchoolService schoolService;

    private final ICourseReservationService courseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(description = "课程查询条件") CourseQuery  query){
        if (query == null){
            return courseService.list();
        }
        QueryChainWrapper<Course> wrapper= courseService.query()
                .eq(query.getType() != null, "type", query.getType())
                .le(query.getEdu() != null, "edu", query.getEdu());
        if(!CollectionUtils.isEmpty(query.getSorts())){
            for (CourseQuery.Sort sort : query.getSorts()){
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询所有学校")
    public List<School> querySchool(){
        return schoolService.list();
    }

    @Tool(description = "生成预约单，返回预约单号")
    public Integer createCourseReservation(@ToolParam(description = "预约课程") String course,
                                           @ToolParam(description = "学生姓名") String studentName,
                                           @ToolParam(description = "联系方式") String contactInfo,
                                           @ToolParam(description = "学校名称") String schoolName,
                                           @ToolParam(description = "备注") String remark){
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setSchool(schoolName);
        courseReservation.setRemark(remark);
        courseReservationService.save(courseReservation);
        return  courseReservation.getId();
    }

}
